//
//  SEGDidPaidViewController.m
//  ExcellentHome
//
//  Created by liangxinqiang on 16/5/18.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGDidPaidViewController.h"
#import "PayListRequestModel.h"
#import "SEGPayListViewController.h"


@interface SEGDidPaidViewController ()
@property(nonatomic, strong) SEGDidPaidViewModel *viewModel;
@end

@implementation SEGDidPaidViewController

#pragma mark - life cycle
- (instancetype)initWithViewModel:(SEGDidPaidViewModel*)viewModel{
    if (self == [super init]) {
        _viewModel = viewModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"支付成功";
    self.view.backgroundColor = kColor_W;
    [self showCustomNavigationBackButton];
    
    [self segInitViews];
    [self bindViewModel];
    
    /*
     // 招商代码，项目已交付注释掉
    if (self.payTypeName == SEGPayTypeName_MonthCardPay){
        NSString *_MID = [self.merchantDic valueForKey:@"MID"];
        NSString *_OrderNo = [self.merchantDic valueForKey:@"OrderNo"];
        NSString *_PayNo = [self.merchantDic valueForKey:@"PayNo"];
        NSString *_PayType = [self.merchantDic valueForKey:@"PayType"];
        NSString *_PayTime = [self.merchantDic valueForKey:@"PayTime"];
        NSString *_Total = [self.merchantDic valueForKey:@"Total"];
        
        [PayListRequestModel requestMonthCardSavePayRecordWithMID:_MID OrderNo:_OrderNo PayNo:_PayNo PayType:_PayType PayTime:_PayTime Total:_Total finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
        }];
    }
    
    if (self.payTypeName == SEGPayTypeName_TempCardPay){
        
        NSString * _PID = [self.merchantDic valueForKey:@"PID"];
        NSString * _OrderNo = [self.merchantDic valueForKey:@"OrderNo"];
        NSString * _PlateNum = [self.merchantDic valueForKey:@"PlateNum"];
        NSString * _PayNo = [self.merchantDic valueForKey:@"PayNo"];
        NSString * _PayType = [self.merchantDic valueForKey:@"PayType"];
        NSString * _PayTime = [self.merchantDic valueForKey:@"PayTime"];
        NSString * _Coupon = [self.merchantDic valueForKey:@"Coupon"];
        NSString * _Total = [self.merchantDic valueForKey:@"Total"];
        NSString * _Receivable = [self.merchantDic valueForKey:@"Receivable"];
        NSString * _CardNo = [self.merchantDic valueForKey:@"CardNo"];
        NSString * _Operator = [self.merchantDic valueForKey:@"Operator"];
        
        [PayListRequestModel requestSavePayRecordWithPlatePID:_PID OrderNo:_OrderNo PlateNum:_PlateNum PayNo:_PayNo PayType:_PayType PayTime:_PayTime Coupon:_Coupon Total:_Total Receivable:_Receivable CardNo:_CardNo Operator:_Operator finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
            
        }];
    }*/
}

#pragma mark - Init Views
-(void)segInitViews{
    // 初始化UI
    if (self.viewModel.payTypeName == SEGPayTypeName_Bill) {
        self.navDisplayType = SEGNavigationBarDisplayTypeHide;
        [self initPayedResponseBillPay];
    }else{
     [self segInitDefaultSubView];
    }
}

- (void)segInitDefaultSubView
{
    UIScreen* scr = [UIScreen mainScreen];
    
    CGRect appbounds = CGRectMake(0, 0, scr.bounds.size.width, scr.bounds.size.height - kNavigationBarAndStatusBarHeight);
    
    UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"buyok_pic_check")];
    UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake((appbounds.size.width - image.size.width) / 2, 64, image.size.width, image.size.height)];
    imageView.image = image;
    [self.view addSubview:imageView];
    
    UILabel* succeed = [[UILabel alloc] initWithFrame:CGRectMake((appbounds.size.width - 100) / 2, imageView.frame.origin.y + image.size.height + 18, 100, 15)];
    succeed.text = @"支付成功";
    succeed.font = [UIFont systemFontOfSize:16];
    succeed.textColor = kColor_2;
    succeed.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:succeed];
}

- (void)initPayedResponseBillPay
{
    self.view.backgroundColor = kColor_W;
    
    // 支付成功的图标
    UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_success")]];
    [self.view addSubview:img];
    
    //支付成功描述
    UILabel* lb = [[UILabel alloc] init];
    lb.text = @"您已支付成功，谢谢！";
    lb.font = kFont_16;
    lb.textColor = kColor_1;
    lb.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:lb];
    
    UIButton* backHomeBtn = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"返回首页" forState:UIControlStateNormal];
        [button setTitleColor:kColor_1 forState:UIControlStateNormal];
        button.titleLabel.font = kFont_16;
        [button addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.cornerRadius = 2;
        button.layer.masksToBounds = YES;
        button.layer.borderWidth = 1;
        button.layer.borderColor = kColor_C.CGColor;
        [self.view addSubview:button];
        button;
    });
    
    UIButton *payFeeListBtn = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"查看缴费记录" forState:UIControlStateNormal];
        [button setTitleColor:kColor_W forState:UIControlStateNormal];
        button.titleLabel.font = kFont_16;
        [button setBackgroundImage:[UIImage imageWithColor:kColor_T] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithColor:kColor_T_Press] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(checkPayFeeListButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.cornerRadius = 2;
        button.layer.masksToBounds = YES;
        [self.view addSubview:button];
        button;
    });
    
    [img sizeToFit];
    [img mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).mas_offset(104);
        make.height.width.mas_equalTo(46);
    }];
    
    [lb mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(img.mas_bottom).mas_offset(12);
    }];
    
    [backHomeBtn mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.equalTo(lb.mas_bottom).mas_offset(80);
        make.left.equalTo(self.view).mas_offset(50);
        make.right.equalTo(self.view).mas_offset(-50);
        make.height.mas_equalTo(44);
    }];
    
    [payFeeListBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(backHomeBtn);
        make.top.equalTo(backHomeBtn.mas_bottom).offset(10);
    }];
}

#pragma mark - Private Methods

/// push VC
/// @param vc vc
-(void)pushToCurrentViewController:(UIViewController*)vc {
    
    NSMutableArray *vcArr = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    
    for (NSInteger i = vcArr.count -1; i >= 0; i --) {
        UIViewController *indexVC = vcArr[i];
        
        if ([NSStringFromClass(indexVC.class) isEqualToString:NSStringFromClass(self.class)] ||
            [NSStringFromClass(indexVC.class) isEqualToString:NSStringFromClass(SEGPayListViewController.class)]) {
            [vcArr removeObjectAtIndex:i];
        }
    }
    
    [vcArr addObject:vc];
    [self.navigationController setViewControllers:vcArr animated:YES];
    
}


/// pop 到发起支付的页面
-(void)popToSenToPayController {
    UIViewController *lastVC;
    BOOL hasPayListVC = NO;
    for (UIViewController *object in self.navigationController.viewControllers) {
        if ([NSStringFromClass(object.class) isEqualToString:NSStringFromClass(SEGPayListViewController.class)]) {
            hasPayListVC = YES;
            break;
        }
        lastVC = object;
    }
    
    if (hasPayListVC && lastVC) {
        [self.navigationController popToViewController:lastVC animated:NO];
    }else{
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

#pragma mark - 按钮点击事件

- (void)backClick:(UIButton*)sender {
    [self popToSenToPayController];
}

- (void)checkPayFeeListButtonClick:(UIButton *)sender {
    UIViewController *billPaymentRecord = [[SEGUhomeMediator sharedInstance] seg_initBillPaymentRecordViewController:self.viewModel.dataDic];
    [self pushToCurrentViewController:billPaymentRecord];
}

- (void)onNavigationLeftButtonClicked:(UIButton*)sender {
    [self popToSenToPayController];
}

#pragma mark - RAC Data Binding
- (void)bindViewModel {}

#pragma mark - getter
-(SEGDidPaidViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[SEGDidPaidViewModel alloc]init];
    }
    return _viewModel;
}
#pragma mark setter
@end
